package vue;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

public class AddContact extends JDialog{
	
	private JLabel name, lastname, home, comp, pers, pro, page, mail,photo,groupe;
	private JTextField tname, tlastname, thome, tcomp, tpers, tpro, tpage,tmail;
	private List<String> tab ;
	private JButton btnPhoto;
	private JComboBox cbGroup;
	
	
	/**
	 * Constructor
	 * @param parent
	 * @param title
	 * @param modal
	 */
	public AddContact(JFrame parent, String title, boolean modal) {
		 super(parent, title, modal);
		 this.setSize(340, 400);
		 this.setLocationRelativeTo(null);
		 this.setResizable(false);
		 this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		 tab = new ArrayList<String>();
		 this.init();
		 this.setVisible(true);
		 
	}  
	
	/**
	 * initialize graphic components
	 */
	private void init(){
		 JPanel pan = new JPanel();		 
		 name = new JLabel(AddressBook.FIRSTNAME);
		 name.setPreferredSize(new Dimension(130, 20));
		 lastname = new JLabel(AddressBook.LASTNAME);
		 lastname.setPreferredSize(new Dimension(130, 20));
		 home = new JLabel(AddressBook.HOME);
		 home.setPreferredSize(new Dimension(130, 20));
		 comp = new JLabel(AddressBook.COMPANY);
		 comp.setPreferredSize(new Dimension(130, 20));
		 pers = new JLabel(AddressBook.PERS);
		 pers.setPreferredSize(new Dimension(130, 20));
		 pro = new JLabel(AddressBook.PRO);
		 pro.setPreferredSize(new Dimension(130, 20));
		 page = new JLabel(AddressBook.PAGE);
		 page.setPreferredSize(new Dimension(130, 20));
		 mail = new JLabel(AddressBook.EMAIL);
		 mail.setPreferredSize(new Dimension(130, 20));
		 groupe = new JLabel(AddressBook.GROUP);
		 groupe.setPreferredSize(new Dimension(130, 20));
		 photo = new JLabel();
		 photo.setPreferredSize(new Dimension(130, 20));
		 tname = new JTextField();
		 tname.setPreferredSize(new Dimension(130, 20));
		 tlastname = new JTextField();
		 tlastname.setPreferredSize(new Dimension(130, 20));
		 thome = new JTextField();
		 thome.setPreferredSize(new Dimension(130, 20));
		 tcomp = new JTextField();
		 tcomp.setPreferredSize(new Dimension(130, 20));
		 tpers = new JTextField();
		 tpers.setPreferredSize(new Dimension(130, 20));
		 tpro = new JTextField();
		 tpro.setPreferredSize(new Dimension(130, 20));
		 tpage = new JTextField();
		 tpage.setPreferredSize(new Dimension(130, 20));
		 tmail = new JTextField();
		 tmail.setPreferredSize(new Dimension(130, 20));
		 btnPhoto = new JButton("Select photo");
		 btnPhoto.setPreferredSize(new Dimension(130, 30));
		 cbGroup = new JComboBox();
		 cbGroup.addItem(AddressBook.FAMILY);
		 cbGroup.addItem(AddressBook.FRIEND);
		 cbGroup.addItem(AddressBook.WORK);
		 cbGroup.setPreferredSize(new Dimension(130, 20));
		 pan.add(name);
		 pan.add(tname);
		 pan.add(lastname);
		 pan.add(tlastname);
		 pan.add(home);
		 pan.add(thome);
		 pan.add(comp);
		 pan.add(tcomp);
		 pan.add(pers);
		 pan.add(tpers);
		 pan.add(pro);
		 pan.add(tpro);
		 pan.add(page);
		 pan.add(tpage);
		 pan.add(mail);
		 pan.add(tmail);
		 pan.add(groupe);
		 pan.add(cbGroup);
		 pan.add(photo);
		 pan.add(btnPhoto);
		 
		btnPhoto.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser chooser = new JFileChooser();
				    FileNameExtensionFilter filter = new FileNameExtensionFilter(
				        "JPG", "jpg");
				    chooser.setFileFilter(filter);
				    int returnVal = chooser.showOpenDialog(null);
				    if(returnVal == JFileChooser.APPROVE_OPTION) {
				       photo.setText(chooser.getSelectedFile().getPath());
				    }
			}
		});
		 
		 
		 
		 JButton create = new JButton("CREATE");
		 create.setPreferredSize(new Dimension(130, 50));
		 pan.add(create);
		 create.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					tab.add(tname.getText());
			         tab.add(tlastname.getText());
			         tab.add(thome.getText());
			         tab.add(tcomp.getText());
			         tab.add(tpers.getText());
			         tab.add(tpro.getText());
			         tab.add(tpage.getText());
			         tab.add(tmail.getText());
			         tab.add(photo.getText());
			         tab.add((String)cbGroup.getSelectedItem());
			         setVisible(false);
				}
			     });
		 this.getContentPane().add(pan);
	}

	/**
	 * @return the tab
	 */
	public List<String> getTab() {
		return tab;
	}


}
